package br.ykhan.sql.sqandal.joins;

import br.ykhan.sql.sqandal.elements.Column;
import br.ykhan.sql.sqandal.elements.Table;

// FIXME : Mudar completamente como os Joins funcionam

public class Join {

	private JoinType type;
	private Table tableJoined;
	private Column columnFromTable;
	private Column columnForTesting;

	public Join(JoinType type, Table tableJoined, 
			Column columnFromTable,	Column columnForTesting) {
		this.type = type;
		this.tableJoined = tableJoined;
		this.columnFromTable = columnFromTable;
		this.columnForTesting = columnForTesting;
	}
	
	/**
	 * Retorna String com o nome da declaração do Join.<p>
	 * Exemplo:<br>
	 * <b>INNER JOIN Gender AS g ON g.Id = Person.IdGender</b>
	 */	
	public String getDeclarationName(){
		StringBuffer str = new StringBuffer();
		
		str.append(this.type.getText()+" ");
		str.append(this.tableJoined.getDeclarationName()+" ");
		str.append("ON "+this.columnFromTable.getReferenceName()+" = "+this.columnForTesting.getReferenceName());
		
		return str.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if(!(obj instanceof Join)) return false;
		
		Join j = (Join)obj;		
		
		return this == j || (
			this.tableJoined.equals(j.getTableJoined()) && 
			this.columnForTesting.equals(j.columnForTesting) && 
			this.columnFromTable.equals(j.columnFromTable));
			
	}

	// Getters and Setters
	
	public JoinType getType() { return type; }
	public void setType(JoinType type) { this.type = type; }
	
	public Table getTableJoined() { return tableJoined; }
	public void setTableJoined(Table tableJoined) { this.tableJoined = tableJoined; }
	
	public Column getColumnFromTable() { return columnFromTable; }
	public void setColumnFromTable(Column columnFromTable) { this.columnFromTable = columnFromTable; }
	
	public Column getColumnForTesting() { return columnForTesting; }
	public void setColumnForTesting(Column columnForTesting) { this.columnForTesting = columnForTesting; }

}
